package aplications.android;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

/**
 *  Controller of Aplication
 */
public class Controller extends Activity {

    private Cursor cursor;		//used to access the results from querying for images on the SD card.
    private int columnIndex;	//Column index for the Thumbnails Image IDs.
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.controller);
        
        loadImagesFromSDcard();		 	
        
    }
    
    /** Load and put Images in GridView */
    private void loadImagesFromSDcard() 
    {
    	 String[] projection = {MediaStore.Images.Thumbnails._ID};	// Set up an array of the Thumbnail Image ID column we want
         
         cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,	 // Create the cursor pointing to the SDCard
                 projection, // Which columns to return
                 null,       // Return all rows
                 null,
                 MediaStore.Images.Thumbnails.IMAGE_ID);
         
         columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);	// Get the column index of the Thumbnails Image ID

         GridView sdcardImages = (GridView) findViewById(R.id.gridView);
         sdcardImages.setAdapter(new ImageAdapter(this,cursor,columnIndex));
         
         /**Listener in the images*/
         sdcardImages.setOnItemClickListener(new OnItemClickListener() 
         {
             public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                 // Get the data location of the image
                 String[] projection = {MediaStore.Images.Media.DATA};
                 cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                         projection, // Which columns to return
                         null,       // Return all rows
                         null,
                         null);
                 columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                 cursor.moveToPosition(position);
                 // Get image filename
                 String imagePath = cursor.getString(columnIndex);
                 showImage(imagePath);
             }
         });
	}
    
    /** Puth the path inside Extras and star ColorDetection Activity*/
	private void showImage(String path)
    {
    	 Intent intent = new Intent();
    	 intent.setClass(this,ColorDetection.class);
    	 intent.putExtra("pathImage", path);
    	 startActivity(intent);
    }
	
	/** Method to test the onClick system of the buttons*/
	public void noImage(View button) 
	{
		 Intent intent = new Intent();
		 intent.setClass(this,ColorDetection.class);
		 startActivity(intent);
	}
	
}
